package com.edp.wms.basedata.controller;

import com.edp.wms.basedata.domain.Area;
import com.edp.wms.basedata.service.AreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/basedata/area")
@RequiredArgsConstructor
@Api(tags = "区域管理")
public class AreaController {

    private final AreaService areaService;

    /**
     * 获取所有区域列表
     */
    @GetMapping
    @ApiOperation("获取区域列表")
    public ResponseEntity<List<Area>> getAllAreas() {
        List<Area> areas = areaService.list();
        return ResponseEntity.ok(areas);
    }

    /**
     * 根据ID获取区域详情
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID获取区域详情")
    public ResponseEntity<Area> getAreaById(@PathVariable Long id) {
        Area area = areaService.getById(id);
        if (area != null) {
            return ResponseEntity.ok(area);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 创建新区域
     */
    @PostMapping
    @ApiOperation("创建区域")
    public ResponseEntity<Area> createArea(@RequestBody Area area) {
        // 检查必要字段
        if (area.getAreaCode() == null || area.getAreaName() == null) {
            return ResponseEntity.badRequest().build();
        }

        // 设置默认状态
        if (area.getStatus() == null) {
            area.setStatus("0");
        }

        boolean saved = areaService.save(area);
        if (saved) {
            Area savedArea = areaService.getById(area.getId());
            return ResponseEntity.status(HttpStatus.CREATED).body(savedArea);
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 更新区域信息
     */
    @PutMapping("/{id}")
    @ApiOperation("更新区域信息")
    public ResponseEntity<Area> updateArea(@PathVariable Long id, @RequestBody Area area) {
        // 检查要更新的区域是否存在
        Area existingArea = areaService.getById(id);
        if (existingArea == null) {
            return ResponseEntity.notFound().build();
        }

        // 设置ID确保更新正确的记录
        area.setId(id);

        // 执行更新操作
        boolean updated = areaService.updateById(area);
        if (updated) {
            // 返回更新后的完整对象
            Area updatedArea = areaService.getById(id);
            return ResponseEntity.ok(updatedArea);
        } else {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 删除区域
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除区域")
    public ResponseEntity<Void> deleteArea(@PathVariable Long id) {
        boolean deleted = areaService.removeById(id);
        if (deleted) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
